#pragma once
#include <allegro5/allegro_primitives.h>
//#include <allegro5/allegro_image.h>
//#include <allegro5/allegro_ttf.h>
#include "Map.h"
#include "dataTypes.h"
#include "Color.h"
#include "TileDrawer.h"
#include "CharacterDrawer.h"
using namespace std;

class Map;

class MapDrawer{
public:
	MapDrawer(Map* aMap, int aScreenW, int aScreenH, int aTileSize);
	~MapDrawer();

	Map* GetMap() {return _Map;};
	int GetPanX() {return _panX;};
	int GetPanY() {return _panY;};
	int GetScreenW() {return _screenW;};
	int GetScreenH() {return _screenH;};
	int GetTileSize() {return _tileSize;};

	void pan(EDir dir, int aSpeed = 0);
	void SetPan(int aX, int aY);
	void mouseMiniMapPan(int aX, int aY);
	void zoom(bool aZoom, float aStep = -1);
	void SetZoom(float aZoom);
	void toggleGrid(){_isGrid = _isGrid ? false : true;};
	void toggleMinimap(){_isMinimap = _isMinimap ? false : true;};
	bool IsHeroLocked() {return heroLocked;};
	void SetHeroLock(bool aLock);

	void refresh();
	void resize(int aScreenW, int aScreenH);
	void draw();

private:
	Map* _Map;

	int _screenW;
	int _screenH;
	int _panX;
	int _panY;
	float _zoom;
	int _tileSize;
	bool _isGrid;
	bool _isMinimap;
	bool heroLocked;
	bool justRefreshed;

	// Almost singleton for color picking
	Color color;

	// Main bitmap rendered to the screen
	ALLEGRO_BITMAP* _render;

	// Light mask
	ALLEGRO_BITMAP* lightMap;

	// MiniMap bitmap rendered to the _render
	ALLEGRO_BITMAP* _miniMap;

	// Tile transitions b&w array
	ALLEGRO_BITMAP* _transitions[32];

	// Just a helper array od *pointers* when scrolling a _renderTiles
	vector<ALLEGRO_BITMAP*> _scrollTemp;

	// Small tiles that are actually on the screen
	// while panning, pointers in this table are scrolled
	Tab2D<ALLEGRO_BITMAP*> _renderTiles;

	// Big trees vector
	// I don't have a good idea how to do it better
	Tab2D<ALLEGRO_BITMAP*> bigTreeTiles;

	// Vector for character bitmaps
	vector<ALLEGRO_BITMAP*> characters;
	vector<ALLEGRO_BITMAP*> characterNames;

	TileDrawer _Drawer;
	CharacterDrawer characterDrawer;

	// Core
	void prepareTransitions();
	void calculateTransitions(int aLower, int aX, int aY, int& aEdges, int& aCorners);
	void scrollRender(int aDir);
	void moveRender(int aX, int aY);
	void drawGrid(int aX1, int aY1, int aX2, int aY2);
	void drawTile(int aX, int aY);
	void render();
		void DrawQuestMarker(int aX, int aY, ALLEGRO_COLOR aColor);

	// Critical
	void drawTransitions(int aX, int aY);
	void drawLower(int aX, int aY);
	void drawUpper(int aX, int aY);
	void drawMinimap();

	// World Map
	void drawLowerWorld(int aX, int aY);
	void drawUpperWorld(int aX, int aY);
	ALLEGRO_COLOR getMinimapColorWorld(Tile aTile);

	// City Map
	void drawLowerCity(int aX, int aY);
	void drawUpperCity(int aX, int aY);
	ALLEGRO_COLOR getMinimapColorCity(Tile aTile, EBiome aBiome);
};

